/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package telas;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import usuario.Usuario;
import usuario.UsuarioDAO;
import usuario.UsuarioModal;

/**
 *
 * @author Max Weber
 */
public class O6TelaVerPerfilDeUsuario extends javax.swing.JDialog {

    Usuario usuario;
    boolean cadastrado;
    /**
     * Creates new form O6VerPerfilDeUsuario
     */
    
    public O6TelaVerPerfilDeUsuario() {
        initComponents();
        
    }
    
    public O6TelaVerPerfilDeUsuario(Usuario usuarios){
        initComponents();
        setModal(true);
        this.usuario= usuarios;
        tfNome.setText(usuario.getNome_usuario());
        tfLogin.setText(usuario.getLogin());
        jcTipoDeUsuario.setSelectedItem(usuario.getTipo());
        tfNome.setEnabled(false);
        tfLogin.setEnabled(false);
        jcTipoDeUsuario.setEnabled(false);
        btSalvar.setEnabled(false);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        btSalvar = new javax.swing.JLabel();
        jPanel9 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        btCancelar = new javax.swing.JLabel();
        tfNome = new javax.swing.JTextField();
        tfLogin = new javax.swing.JTextField();
        jcTipoDeUsuario = new javax.swing.JComboBox();
        jPanel10 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        btEditar = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SISVATEC - Sistema de Vacinação");
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBackground(new java.awt.Color(0, 114, 180));
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jLabel1.setFont(new java.awt.Font("Arial", 1, 16)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("   Ver perfil de usuário");
        jPanel2.add(jLabel1);

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 550, 30));

        jLabel2.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        jLabel2.setText("Nome do usuário:");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 60, -1, -1));

        jLabel4.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        jLabel4.setText("Login:");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 130, -1, -1));

        jLabel6.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        jLabel6.setText("Tipo de usuário:");
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 210, -1, -1));

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/ver_perfi2.png"))); // NOI18N
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 50, -1, -1));

        jPanel8.setBackground(new java.awt.Color(10, 66, 108));
        jPanel8.setLayout(new java.awt.GridBagLayout());

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/SAVE.png"))); // NOI18N
        jPanel8.add(jLabel8, new java.awt.GridBagConstraints());

        btSalvar.setFont(new java.awt.Font("Arial", 1, 16)); // NOI18N
        btSalvar.setForeground(new java.awt.Color(255, 255, 255));
        btSalvar.setText("  Salvar");
        btSalvar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btSalvarMouseClicked(evt);
            }
        });
        jPanel8.add(btSalvar, new java.awt.GridBagConstraints());

        jPanel1.add(jPanel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 310, 100, 30));

        jPanel9.setBackground(new java.awt.Color(10, 66, 108));
        jPanel9.setLayout(new java.awt.GridBagLayout());

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/fecharBT.png"))); // NOI18N
        jPanel9.add(jLabel5, new java.awt.GridBagConstraints());

        btCancelar.setFont(new java.awt.Font("Arial", 1, 16)); // NOI18N
        btCancelar.setForeground(new java.awt.Color(255, 255, 255));
        btCancelar.setText("  Cancelar");
        btCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btCancelarMouseClicked(evt);
            }
        });
        jPanel9.add(btCancelar, new java.awt.GridBagConstraints());

        jPanel1.add(jPanel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 310, 110, 30));

        tfNome.setFont(new java.awt.Font("Arial", 0, 16)); // NOI18N
        jPanel1.add(tfNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 90, 340, 30));

        tfLogin.setFont(new java.awt.Font("Arial", 0, 16)); // NOI18N
        jPanel1.add(tfLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 160, 240, 30));

        jcTipoDeUsuario.setFont(new java.awt.Font("Arial", 0, 16)); // NOI18N
        jcTipoDeUsuario.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrador", "Comum" }));
        jPanel1.add(jcTipoDeUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 240, 140, 30));

        jPanel10.setBackground(new java.awt.Color(10, 66, 108));
        jPanel10.setLayout(new java.awt.GridBagLayout());

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/edit.png"))); // NOI18N
        jPanel10.add(jLabel7, new java.awt.GridBagConstraints());

        btEditar.setFont(new java.awt.Font("Arial", 1, 16)); // NOI18N
        btEditar.setForeground(new java.awt.Color(255, 255, 255));
        btEditar.setText("  Editar");
        btEditar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btEditarMouseClicked(evt);
            }
        });
        jPanel10.add(btEditar, new java.awt.GridBagConstraints());

        jPanel1.add(jPanel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 310, 110, 30));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 520, 350));

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btSalvarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btSalvarMouseClicked
      
        if(cadastrado){
           usuario = new Usuario(); 
        }
        
        usuario.setNome_usuario(tfNome.getText());
        usuario.setLogin(tfLogin.getText());
        usuario.setTipo(jcTipoDeUsuario.getSelectedItem().toString());
        
        UsuarioDAO dao = new UsuarioDAO();
        dao.atualizar(usuario);
        
        //atualiza
        tfNome.setText(usuario.getNome_usuario());
        tfLogin.setText(usuario.getLogin());
        
        //desabilita campos
        tfNome.setEnabled(false);
        tfLogin.setEnabled(false);
        jcTipoDeUsuario.setEnabled(false);
        btSalvar.setEnabled(false);
        JOptionPane.showMessageDialog(rootPane, "Perfil de usuário atualizado com sucesso!");
        
        O4TelaConsultarUsuarios tela = new O4TelaConsultarUsuarios();
        dispose();
        tela.setVisible(true);
                
        
    }//GEN-LAST:event_btSalvarMouseClicked

    private void btCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btCancelarMouseClicked
        // TODO add your handling code here:
        dispose();
        O4TelaConsultarUsuarios tela = new O4TelaConsultarUsuarios();
        tela.setVisible(true);
        
    }//GEN-LAST:event_btCancelarMouseClicked

    private void btEditarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btEditarMouseClicked
        // TODO add your handling code here:
        btEditar.setEnabled(false);
        btSalvar.setEnabled(true);
        tfNome.setEnabled(true);
        tfLogin.setEnabled(true);
        jcTipoDeUsuario.setEnabled(true);
        
    }//GEN-LAST:event_btEditarMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(O6TelaVerPerfilDeUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(O6TelaVerPerfilDeUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(O6TelaVerPerfilDeUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(O6TelaVerPerfilDeUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new O6TelaVerPerfilDeUsuario().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel btCancelar;
    private javax.swing.JLabel btEditar;
    private javax.swing.JLabel btSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JComboBox jcTipoDeUsuario;
    private javax.swing.JTextField tfLogin;
    private javax.swing.JTextField tfNome;
    // End of variables declaration//GEN-END:variables
}
